/*
 * VendingMachine.java
 */

/**
 *
 * @author Derek Niles
 */

package vendingMachine.gui;

import vendingMachine.localLogger.*;
import vendingMachine.finance.*;
import vendingMachine.inventory.*;
import java.io.IOException;

public class VendingMachine {
    
    private int machineID;
    
    /** Creates a new instance of VendingMachine */
    public VendingMachine() {
    }
    
    /**
     * Returns the unique machine identifier.
     */
    public int getMachineID() {
        return machineID;
    }
    
  /*
   * public void selectItem(InventoryItem item) { if (cart == null) { cart = new
   * ShoppingCart(); } cart.addItem(item); // pass item received to shopping
   * cart }
   *
   * public void removeItem(InventoryItem item) {
   * cart.removeItem(item.getProductID()); // pass item received to shopping
   * cart's remove method }
   *
   * public void completeOrder(PaymentType payment) { // pass on to payment
   * manager }
   *
   * public void cancelOrder() { cart = null; // remove shopping cart. }
   */
    public static void main(String[] args) {
        //VendingMachine vm = new VendingMachine();
        ShoppingCart cart = new ShoppingCart();
        VendingInventory inventory = VendingInventory.getInstance();
        DisplayManager dispManager = new DisplayManager();
        LocalLogger localLogger = new LocalLogger();
        AuthorizeController authControl = new AuthorizeController();
        
        int prodID = 0;
        int cartProdID = 0;
        
        while (true) {
            prodID = dispManager.DisplayInventory(cart);
            //System.out.println(prodID);
            
            if (prodID > 0)
                cart.addItem(inventory.getItem(prodID));
            else if (prodID == -1) {
                cartProdID = dispManager.DisplayShoppingCart(cart);
                if (cartProdID > 0)
                    cart.removeItem(cartProdID);
                else if (cartProdID == -2) {
                    // check out
                    PaymentCallback pcb = new PaymentCallback();
                    authControl.AuthorizeSale(pcb, (float)cart.calculateTotal(), "Credit");
                    System.out.println("checking out");
                    try {
                        while (pcb.keepWaiting()) {
                            Thread.sleep(200);
                        }
                        
                        if (pcb.getAuthorizeObject().authorize()) {
                            inventory.dispenseItemList(cart);
                            localLogger.processSale(cart, pcb.getAuthorizeObject());
                        }
                        cart = new ShoppingCart();
                        
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                    
                    
                } else if (cartProdID == -3)
                    cart = new ShoppingCart();
            } else if (prodID == -3)
                cart = new ShoppingCart();
            else
                break;
            
            prodID = 0;
            cartProdID = 0;
        }
        
    }
}
